﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aij1.Cms;
using Aij1.Utils;
using Aij1.BLL;
using Aij1.Model;
using System.Text;
using System.Data;

namespace Aij1
{
    public partial class AlbumCenter : BasePage
    {
        int catId;
        protected WebPager WPager;
        protected IDataReader Reader;
        protected void Page_Load(object sender, EventArgs e)
        {
            PageName = catId == 0 ? "亲子相册" : XpoSession.GetObjectByKey<A_Category>(catId).Title;
            PageName += (WPager.PageIndex <= 1) ? "" : " 第" + WPager.PageIndex + "页";
            Keywords = "摄影作品,韩框相册,金蚕丝册,动感水晶,琉璃册,精采展示,样册";
            Description = Config.CompanyName + "拍出的样册,8X8,8X12,10X10,20X36,宝宝不需要化妆也不需要PS就很漂亮";
            Reader = AlbumData.GetPagedAlbumsList(WPager);
        }

        protected void Page_init(object sender, EventArgs e)
        {
            catId = CommOp.ToInt(Request.QueryString["Id"]);
            CNM.CategoryId = catId;

            WPager = new WebPagerSEO()
            {
                KeyId = "Oid",
                OrderBy = "SetTopTime DESC,DisplayTime DESC",
                Query = @"SELECT a.Oid,a.Title,a.DisplayTime,a.SetTopTime,p.SnapShotUrl,p.SnapShotWidth,p.SnapShotHeight,c.Oid  as CategoryId,c.Title as CategoryTitle FROM 
A_Article a INNER JOIN A_Photo p ON a.SamplePhotoId=p.Oid INNER JOIN A_Category c ON A.CategoryId=c.Oid WHERE Status=3 AND a.GCRecord IS NULL AND " +
(catId == 0 ? "a.AType=1 AND c.AType=1" : "a.AType=1 AND CategoryId=" + catId),
                PageSize = 16,
            };
        }

        public string GetAlbumListHtml(IDataReader reader, int w, int h)
        {
            StringBuilder sb = new StringBuilder();
            while (reader.Read())
            {
                sb.Append(GetAlbumHtml(reader, w, h));
            }
            return sb.ToString();
        }

        static string format = @"<a href='Gallery_{0}.htm' target='_blank'>
<div style='width:{1}px;height:{2}px;overflow:hidden;margin-bottom:6px' >
<img src='{3}' border='0' style='margin-left:{4}px;margin-top:{5}px;width:{7}px;height:{8}px' alt='{6}' /></div>
{6}</a>";

        string GetAlbumHtml(IDataReader reader, int divW, int divH)
        {
            int marginLeft = (divW - (int)reader["SnapShotWidth"]) / 2;
            int marginTop = (divH - (int)reader["SnapShotHeight"]) / 2;
            return string.Format(format, reader["Oid"], divW, divH, Page.ResolveClientUrl((string)reader["SnapShotUrl"]), marginLeft, marginTop, reader["Title"], reader["SnapShotWidth"], reader["SnapShotHeight"]);
        }

    }
}